<?php
	DEFINE('H1', 'File Writer');
	DEFINE('TITLE','File Writer');
	require('includes/header.php');
?>	
	<h2><?PHP echo H1 ?></h2>

<form method="post" action="filewriter.php">
File Contents:<br />
<textarea name="content" rows="15" cols="50">
<?php
	$myFile='misc/myFile.txt';

	if(isset($_POST['delete']))
	{
		if(file_exists($myFile))
		{
			unlink($myFile);
			unset($_POST['content']);
			$action = "Data deleted";
			$filecontent="";
		}

	}
	else if (isset($_POST['submit']))
	{
		if(file_exists($myFile))
		{
			$myFile= "misc/myFile.txt";
			$contents = $_POST['content'];
			echo "$contents";
			unset($_POST['content']);
			
			file_put_contents($myFile, $contents);
			$action = "Data written to $myFile";
		}
		else {
			$myFile= "misc/myFile.txt";
			fopen($myFile, 'w') or die("Can't open file");
			
			$contents = $_POST['content'];
			echo "$contents";
			unset($_POST['content']);
			
			file_put_contents($myFile, $contents);
			$action = "Data written to $myFile";
		}
		
	}
?>
</textarea>
<br />
<input type="submit" name="submit" value="Write Content" />
<input type="submit" name="delete" value="Delete Content" />

</form>

<?php
	require('includes/footer.php');
?>